<?php

namespace addons\xpframework\library\xcore\xcore\utils;

use addons\xpframework\library\xcore\xcore\XcException;
use think\Request;


class XcRequestPathUtils
{

    /**
     * 完整的绝对地址
        urlBase("/index.html"); // http://www.example.com/index.html
     * @return string
     */
    public static function urlBase($url = '')
    {
        $xcbaseurl = XcConfigUtils::config("xcbaseurl");
        if (empty($xcbaseurl) || $xcbaseurl == 'http://www.example.com/' || $xcbaseurl == 'https://www.example.com/') {
            $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
            $xcbaseurl = $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') ;
        }
        return $xcbaseurl . $url;
    }

    /**
     * 拼接出Admin后台的完整地址
        urlAdmin("/index.html"); // http://www.example.com/abc.php/index.html
     * @return string
     */
    public static function urlAdmin($url = '')
    {
        $request = Request::instance();
        $modulename = $request->module();
        if (empty($modulename)) {
            throw new XcException("配置错误", 0, null, "无法在非管理后台获取管理地址");
        }
        $adminprefix = rtrim(url("/{$modulename}", '', false), '/');
        return static::urlBase($adminprefix . $url);
    }
}
